package com.example.quanlithuchi;

import java.text.SimpleDateFormat;
import java.util.Date;

import InterFaceIModel.ILoaiThuChi;
import InterFaceIModel.LoaiThuChiModel;
import Presenter.ThuChiPresenter;
import View.IthuchiDetailView;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ThuchidetailActivity extends Activity implements IthuchiDetailView {

	TextView txtLoaithuchi;
	Context context=this;
	long idLoadThuchi;
	TextView txtNgayThuChi;
	EditText txtSotien_thuchi,txtGhichu_thuchi,txtKhoanchi;
	Button btnSaveThuchi,btnCancelThuchi;
	ILoaiThuChi loaiThuChi;
	ThuChiPresenter presenter;
	long idthuchi;
	boolean isAdd;
	Date date;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thuchidetail);
		presenter=new ThuChiPresenter(this);
		txtLoaithuchi=(TextView) findViewById(R.id.txtLoaithuchi);
		txtLoaithuchi.setOnTouchListener(onTouchListener);
		txtNgayThuChi=(TextView) findViewById(R.id.txtngay_thuchi);
		txtSotien_thuchi=(EditText) findViewById(R.id.txtsotien_thuchi);
		txtGhichu_thuchi=(EditText) findViewById(R.id.txtghichu_thuchi);
		txtKhoanchi=(EditText) findViewById(R.id.txtkhoanthuchi);
		btnSaveThuchi=(Button) findViewById(R.id.btnSave_thuchi);
		btnCancelThuchi=(Button) findViewById(R.id.btncancel_thuchi);
		btnSaveThuchi.setOnClickListener(onsave_cancelClickListener);
		btnCancelThuchi.setOnClickListener(onsave_cancelClickListener);
		Intent intent=getIntent();
		 isAdd=intent.getBooleanExtra("isAdd", true);
		if (isAdd) {
			date=new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			String ngaynoString = formatter.format(date);
			txtNgayThuChi.setText(ngaynoString);
		} else {
			loaiThuChi=new LoaiThuChiModel();
			String khoanchi=intent.getStringExtra("khoanchi");
			txtKhoanchi.setText(khoanchi);
			double sotien=intent.getDoubleExtra("sotien", 0);
			txtSotien_thuchi.setText(String.valueOf(sotien));
			String tenLoaithuchiString=intent.getStringExtra("tenloaithuchi");
			txtLoaithuchi.setText(tenLoaithuchiString);
			long idloaithuchi=intent.getLongExtra("idloaithuchi", 0);
			int isthuchi=intent.getIntExtra("isthuchi", 0);
			loaiThuChi.setId(idloaithuchi);
			loaiThuChi.setTenLoai(tenLoaithuchiString);
			loaiThuChi.setIsThucThi(isthuchi);
			long ngaychi=intent.getLongExtra("ngaychi", 0);
			date=new Date(ngaychi);
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			String ngaynoString = formatter.format(date);
			txtNgayThuChi.setText(ngaynoString);
			String ghichuString=intent.getStringExtra("ghichu");
			txtGhichu_thuchi.setText(ghichuString);
			idthuchi=intent.getLongExtra("id", 0);
			
		}
		 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_thuchidetail, menu);
		return true;
	}
	OnTouchListener onTouchListener=new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			Intent intent=getIntent();
			int value=intent.getIntExtra("id", 0);
			Intent newIntent=new Intent(context, Loai_thuchi_activity.class);
			newIntent.putExtra("id", value);
			startActivityForResult(newIntent, 1);
			return false;
		}
	};
	
	OnClickListener onsave_cancelClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnSave_thuchi:
			{
			
				if (isAdd) {
					
					long result=	presenter.insert();
					if (result>=0) {
						Log.d("insert", "thanh cong");
						Intent intent=new Intent();
						intent.putExtra("khoanchi", getKhoanchi());
						intent.putExtra("sotien", getSotien());
						intent.putExtra("idloaithuchi", loaiThuChi.getId());
						intent.putExtra("tenloaithuchi", loaiThuChi.getTenLoai());
						intent.putExtra("isthuchi", loaiThuChi.getIsThuchi());
						intent.putExtra("ngaychi", getNgaychi());
						intent.putExtra("ghichu", getGhichu());
						intent.putExtra("id", result);
						setResult(1, intent);
						finish();
						
					}
					
				} else {
                    
					
					int result=presenter.update();
					if (result>0) {
						
						Log.d("update", "thanh cong");
						
						Intent intent=new Intent();
						intent.putExtra("khoanchi", getKhoanchi());
						intent.putExtra("sotien", getSotien());
						intent.putExtra("idloaithuchi", loaiThuChi.getId());
						intent.putExtra("tenloaithuchi", loaiThuChi.getTenLoai());
						intent.putExtra("isthuchi", loaiThuChi.getIsThuchi());
						intent.putExtra("ngaychi", getNgaychi());
						intent.putExtra("ghichu", getGhichu());
						intent.putExtra("id", getId());
						setResult(2, intent);
						finish();
					}
				}
			}
				break;
			case R.id.btncancel_thuchi:
			{
				finish();
			}
				break;

			
			}
		}
	};
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode==1&&resultCode==1) {
			
			idLoadThuchi=data.getLongExtra("idloai", 0);
			String tenloai=data.getStringExtra("tenloai");
			int isthuchi=data.getIntExtra("isthuchi", 0);
			loaiThuChi=new LoaiThuChiModel();
			loaiThuChi.setId(idLoadThuchi);
			loaiThuChi.setTenLoai(tenloai);
			loaiThuChi.setIsThucThi(isthuchi);
			txtLoaithuchi.setText(tenloai);
		}
	}

	@Override
	public String getKhoanchi() {
		// TODO Auto-generated method stub
		return txtKhoanchi.getText().toString();
	}

	@Override
	public double getSotien() {
		// TODO Auto-generated method stub
		try {
			return Double.parseDouble(txtSotien_thuchi.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
			Toast.makeText(context, "Ban phai nhap so tien", Toast.LENGTH_LONG).show();
			return -1;
		}
	}

	

	@Override
	public String getGhichu() {
		// TODO Auto-generated method stub
		return txtGhichu_thuchi.getText().toString();
	}

	

	@Override
	public void setKhoanchi(String khoanchi) {
		// TODO Auto-generated method stub
		txtKhoanchi.setText(khoanchi);
	}

	@Override
	public void setSotien(double sotien) {
		// TODO Auto-generated method stub
		txtSotien_thuchi.setText(String.valueOf(sotien));
	}

	

	@Override
	public void setGhichu(String ghichu) {
		// TODO Auto-generated method stub
		txtGhichu_thuchi.setText(ghichu);
	}

	@Override
	public void setNgaychi(long ngaychi) {
		// TODO Auto-generated method stub
		Date date=new Date(ngaychi);
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String ngaynoString = formatter.format(date);
		txtNgayThuChi.setText(ngaynoString);
	}

	@Override
	public ILoaiThuChi getLoaiThuChi() {
		// TODO Auto-generated method stub
		return loaiThuChi;
	}

	@Override
	public long getNgaychi() {
		// TODO Auto-generated method stub
		return date.getTime();
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return context;
	}

	@Override
	public long getId() {
		// TODO Auto-generated method stub
		return idthuchi;
	}
    
}
